<template>
  <el-dialog title="库存物资选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="80%"
    center
    @close="cancel"
  >
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="itemTypeName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="itemTypeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">

      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="物资编码" prop="itemCode">
              <el-input
                v-model="queryParams.itemCode"
                placeholder="请输入产品物料编码"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物资名称" prop="itemName">
              <el-input
                v-model="queryParams.itemName"
                placeholder="请输入产品物料名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="批次号" prop="batchCode">
              <el-input
                v-model="queryParams.batchCode"
                placeholder="请输入批次号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="仓库名称" prop="warehouseName">
              <el-input
                v-model="queryParams.warehouseName"
                placeholder="请输入仓库名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商编号" prop="vendorCode">
              <el-input
                v-model="queryParams.vendorCode"
                placeholder="请输入供应商编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商名称" prop="vendorName">
              <el-input
                v-model="queryParams.vendorName"
                placeholder="请输入供应商名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="wmstockList"
        border
        @current-change="handleCurrent"
        @row-dblclick="handleRowDbClick">
        <el-table-column  width="55" align="center" >
            <template v-slot="scope">
                <el-radio v-model="selectedId" :label="scope.row.materialStockId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
            </template>
        </el-table-column>
        <el-table-column label="产品物料编码" width="120px" align="center" prop="itemCode"/>
        <el-table-column label="产品物料名称" width="150px" align="center" prop="itemName" :show-overflow-tooltip="true"/>
        <el-table-column label="规格型号" align="center" prop="specification" :show-overflow-tooltip="true"/>
<!--        <el-table-column label="单位" align="center" prop="unitOfMeasure" />-->
        <el-table-column label="单价" align="center" prop="price" />
        <el-table-column label="入库批次号" width="100px" align="center" prop="batchCode" :show-overflow-tooltip="true"/>
        <el-table-column label="仓库" align="center" prop="warehouseName" />
        <el-table-column label="库区" align="center" prop="locationName" />
        <el-table-column label="库位" align="center" prop="areaName" />
        <el-table-column label="单位" align="center" prop="unitOfMeasure" />
        <el-table-column label="在库数量(主)" align="center" prop="quantityOnhand">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.quantityOnhand">{{ scope.row.quantityOnhand }}</el-tag>
            <span v-else>{{ scope.row.quantityOnhand }}</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商编号" width="100px" align="center" prop="vendorCode" />
        <el-table-column label="供应商名称" width="120px" align="center" prop="vendorName" :show-overflow-tooltip="true"/>
        <el-table-column label="供应商简称" width="100px" align="center" prop="vendorNick" />
        <el-table-column label="客户编码" width="100px" align="center" prop="clientCode" />
        <el-table-column label="客户名称" width="120px" align="center" prop="clientName" :show-overflow-tooltip="true"/>
        <el-table-column label="客户简称" width="100px" align="center" prop="clientNick" />
        <el-table-column label="生产工单号" width="150px" align="center" prop="workorderCode" />
        <el-table-column label="库存有效期" align="center" prop="expireDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-for="col in columnsConfig"
          :key="col.prop"
          :label="col.label"
          :prop="col.prop"
          :width="col.width"
          :align="col.align"
          v-if="col.show">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="出库数量（主）"
          align="center"
          width="150">
          <template slot-scope="scope">
            <div v-if="scope.row.materialStockId === selectedId" style="display: flex; align-items: center">
              <el-input-number
                v-model="scope.row.quantityOutPrimary"
                :min="0"
                :step="0.01"
                :precision="2"
                :disabled="enableMultiUnit === 'Y' && storageUnitType ==='fixed'"
                @input="handlePrimaryChange(scope.row)"
                controls-position="right"
                size="small"        style="flex: 1; margin-right: 5px"
              />
            </div>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="出库数量（辅）"
          align="center"
          width="150"
        v-if="enableMultiUnit === 'Y'">
          <template slot-scope="scope">
            <div v-if="scope.row.materialStockId === selectedId" style="display: flex; align-items: center">
              <el-input-number
                v-model="scope.row.quantityOutWarehouse"
                :min="0"
                :step="0.01"
                :precision="2"
                controls-position="right"
                size="small"        style="flex: 1; margin-right: 5px"
                @input="handleWarehouseChange(scope.row)"
              />
            </div>
            <span v-else>--</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {listStock, listWmstock} from "@/api/mes/wm/wmstock";
import { treeselect } from "@/api/mes/md/itemtype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";

export default {
  name: "WmstockMultiSelect",
  components: { Treeselect },
  data() {
    return {
      columnsConfig:[],
      showFlag:false,
      // 遮罩层
      loading: true,
      // 选中数组
      selectedId: null,
      selectedRow: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      itemTypeName: undefined,
      // 总条数
      total: 0,
      //物料产品分类树
      itemTypeOptions: undefined,
      // 库存记录表格数据
      wmstockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemTypeId: null,
        itemTypeName: null,
        itemId: this.itemId,
        itemCode: this.itemCode,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        price: null,
        batchCode: this.batchCode,
        warehouseId: this.warehouseId,
        warehouseCode: this.warehouseCode,
        warehouseName: null,
        locationId: this.locationId,
        locationCode: this.locationCode,
        locationName: null,
        areaId: this.areaId,
        areaCode: this.areaCode,
        areaName: null,
        vendorId: this.vendorId,
        vendorCode: null,
        vendorName: null,
        vendorNick: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        workorderId: this.workorderId,
        quantityOnhand: null,
        expireDate: null,
        clientNick: null,
      },
      // 表单参数
      form: {},
    };
  },
  props:{
    vendorId: null,
    batchCode: null,
    warehouseId: null,
    warehouseCode: null,
    locationId: null,
    locationCode: null,
    areaId: null,
    areaCode: null,
    workorderId: null,
    itemId: null,
    itemCode: null,
    enableMultiUnit: null,
    // selectedMax:null,
    storageUnitType:null,
    storageRatio:null,
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getItemBase();
  },
  watch: {
    itemCode: {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.queryParams.itemCode = newVal
          this.queryParams.pageNum = 1
          this.getList()
        }
      }
    }
  },
  methods: {
    /** 查询库存记录列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        if(response.rows){
          let rows = response.rows;
          if(this.warehouseCode != null && this.warehouseCode != undefined && this.warehouseCode.indexOf('VIR') >0 ){
            this.wmstockList = response.rows;
          }else {
            //如果不是查询线边库的物资，则在查询结果中过滤掉线边库的数据
            this.wmstockList = response.rows.filter((el) =>{
              return el.warehouseCode.indexOf('VIR') == -1;
            });
          }
          // 新增：初始化出库数量字段
          rows.forEach(row => {
            this.$set(row, 'quantityOutPrimary', null);
            this.$set(row, 'quantityOutWarehouse', null);
          });

          this.wmstockList = rows;
          this.total = response.total;
          this.loading = false;
        }
      });
    },
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
    // 处理主单位变动
    handlePrimaryChange(row){
    //
      if('Y' == this.enableMultiUnit) {
        if (this.storageUnitType === 'fixed') {
          // 辅单位数量=主数量 / 换算比例
          row.quantityOutWarehouse = parseFloat(
              (row.quantityOutPrimary / this.storageRatio).toFixed(7)
          );
        }
        //浮动换算就得 填写两个数量计算出浮动率就可以了
        if (this.storageUnitType === 'float') {
          if (row.quantityOutPrimary > 0 && row.quantityOutWarehouse > 0) {
            // 计算新换算比例 = 主数量 / 库存单位数量
            row.storageRatio = parseFloat(
                (row.quantityOutPrimary / row.quantityOutWarehouse).toFixed(4)
            );
          }
        }
        if (row.quantityOutPrimary > row.quantityOnhand) {
          this.$modal.msgError("当前出库数量（主）大于库存数量");
          return
        }
      }
    },
    // 处理辅单位变动
    handleWarehouseChange(row) {
      if (this.storageUnitType === 'fixed') {
        // 计算主单位数量 = 辅单位数量 * 比例
        let primaryValue = row.quantityOutWarehouse * this.storageRatio;

        // 主数量 = 库存单位数量 * 换算比例
        row.quantityOutPrimary = parseFloat(
            (row.quantityOutWarehouse * this.storageRatio).toFixed(7)
        );
      }
      //浮动换算就得 填写两个数量计算出浮动率就可以了
      if (this.storageUnitType === 'float') {
        if (row.quantityOutPrimary > 0 && row.quantityOutWarehouse > 0) {
          // 计算新换算比例 = 主数量 / 库存单位数量
          row.storageRatio = parseFloat(
              (row.quantityOutPrimary / row.quantityOutWarehouse).toFixed(4)
          );
        }
      }
      if(row.quantityOutPrimary > row.quantityOnhand){
        this.$modal.msgError("当前出库数量（主）大于库存数量");
        return
      }
    },

    /** 查询分类下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.itemTypeOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
        if(row){
            this.selectedRow = row;
        }
    },
    //行双击选中
    handleRowDbClick(row){
      if(row){
        this.selectedRow = row;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRow = row;
      }
    },
    cancel(){
      this.showFlag=false
      this.selectedId=null
      this.selectedRow=null
    },

    //确定选中
    confirmSelect() {
      if (this.selectedId == null || this.selectedId == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }

      // 启用多单位时的校验逻辑
      if (this.enableMultiUnit === 'Y') {
        const row = this.selectedRow;
        const errors = [];

        if (!row.quantityOutPrimary || row.quantityOutPrimary <= 0) {
          errors.push('主单位出库数量必须大于0');
        }

        if (!row.quantityOutWarehouse || row.quantityOutWarehouse <= 0) {
          errors.push('仓储单位出库数量必须大于0');
        }

        if (errors.length > 0) {
          this.$notify({
            title: '错误',
            type: 'error',
            message: errors.join(' ')
          });
          return;
        }
      }

      this.$emit('onSelected', this.selectedRow);
      this.showFlag = false;
      this.selectedRow.quantityOutPrimary = null;
      this.selectedRow.quantityOutWarehouse = null;
      this.selectedRow=null
      this.selectedId=null
    }
  }
};
</script>

<style scoped>
.head-container {
  max-height: calc(100vh - 180px);
  overflow-y: auto;
  padding-right: 8px; /* 给滚动条留出空间 */
}
</style>
